//
//  BN_ChatViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-3-28.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_ChatViewController.h"
#import "BN_FaceChatViewController.h"

@interface BN_ChatViewController () {
    BN_ChatHostPageData *chatHostPageData;
    MJRefreshHeaderView *header_;
}

@end

@implementation BN_ChatViewController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [chatHostPageData.chatHelper cancelQueryChatDetailList];
    [chatHostPageData.chatHelper cancelQueryChatPersonList];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
    
    // NSUserDefault
    [[NSUserDefaults standardUserDefaults]setInteger:10 forKey:USERDEFAULTS_KINDERSECTION];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // UI
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"selectperson"] style:UIBarButtonItemStylePlain target:self action:@selector(selectPersonAction)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    // UITableView
    self.chatTableView = [[UITableView alloc]init];
    if (!chatHostPageData) {
        chatHostPageData = [[BN_ChatHostPageData alloc]init];
    }
    [chatHostPageData toChatListReload:^(BOOL isReload) {
        if (isReload) {
            [self cancelHintView];
            [self.chatTableView reloadData];
        }
    }];
    switch (MAIN_VERSION) {
        case 6:
            self.chatTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44-HEIGHT_TAB_BAR);
            break;
        default:
            self.chatTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
            self.chatTableView.contentInset = UIEdgeInsetsMake(64, 0, HEIGHT_TAB_BAR, 0);
            self.chatTableView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, HEIGHT_TAB_BAR, 0);
            break;
    }
    self.chatTableView.backgroundColor = [UIColor clearColor];
    self.chatTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.chatTableView.delegate = chatHostPageData;
    self.chatTableView.dataSource = chatHostPageData;
    [self setExtraCellLineHidden:_chatTableView];
    
    // 下拉刷新
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.chatTableView;
    header.delegate = chatHostPageData;
    header_ = header;
    [self.view addSubview:self.chatTableView];
    
    // 小球
    [self viewLoadHintView];
    
    // 通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushFaceChatAction:) name:KPUSHFACECHATVC object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lookPersonInfo:) name:K_CHAT_LOOK_PERSONALINFO object:nil];
}


#pragma mark
#pragma mark Action

- (void)lookPersonInfo:(NSNotification *)notification {
    // BN_TeacherMeansViewController *teacherMeansVc = [[BN_TeacherMeansViewController alloc]init];
    // [self.navigationController pushViewController:teacherMeansVc animated:YES];
}

// 选择联系人界面
- (void)selectPersonAction {
    BN_SelectPersonViewController *selectPersonListVC = [[BN_SelectPersonViewController alloc]init];
    selectPersonListVC.delegate = self;
    UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:selectPersonListVC];
    [self.navigationController presentViewController:navCtrl animated:YES completion:NULL];
}

- (void)pushFaceVc:(NSDictionary *)dict {
    BN_FaceChatViewController *faceChatVC = [[BN_FaceChatViewController alloc]init];
    faceChatVC.receiveDict = dict;
    [self.navigationController pushViewController:faceChatVC animated:YES];
}

- (void)pushFaceChatAction:(NSNotification *)notification {
    BN_FaceChatViewController *faceChatVC = [[BN_FaceChatViewController alloc]init];
    faceChatVC.receiveDict = notification.object;
    [self.navigationController pushViewController:faceChatVC animated:YES];
}

- (id)getCurNavController{
    UINavigationController* navController = (UINavigationController*)self.tabBarController.selectedViewController;
    return navController;
}

- (void)dealloc
{
    // code...
}

@end
